﻿package com.wxsoft.business.physic.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.physic.dao.IPsPhysicFactoryDictDao;
import com.wxsoft.business.physic.pojo.PsPhysicFactoryDict;
import com.wxsoft.business.physic.service.IPsPhysicFactoryDictService;
import com.wxsoft.util.common.HanyuPinyinHelper;
import com.wxsoft.util.wx.UUIDUtil;

@Service("psPhysicFactoryDictService")
public class PsPhysicFactoryDictServiceImpl implements IPsPhysicFactoryDictService  {
	@Resource
	private IPsPhysicFactoryDictDao dao;
	
	
	public long findCount(PsPhysicFactoryDict psPhysicFactoryDict) {
		return dao.findCount(psPhysicFactoryDict);
	}
	
	public List<PsPhysicFactoryDict> findAll(PageHelper page,PsPhysicFactoryDict psPhysicFactoryDict) {
        List<PsPhysicFactoryDict> r = dao.findAll(page,psPhysicFactoryDict);
		return r;
	}
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public String add(PsPhysicFactoryDict psPhysicFactoryDict) {
		psPhysicFactoryDict.setId(UUIDUtil.getUUID());

		check(psPhysicFactoryDict);

		//设置汉语拼音
		psPhysicFactoryDict.setMnemonic(HanyuPinyinHelper.getFirstLettersLo(psPhysicFactoryDict.getFactory_name()));
		
		
		int r = this.dao.insert(psPhysicFactoryDict);
		if(r==1){
			return "操作成功!";
		}
		return "操作失败!";
	}
	
	public String modify(PsPhysicFactoryDict psPhysicFactoryDict) {

		check(psPhysicFactoryDict);

		//设置汉语拼音
		psPhysicFactoryDict.setMnemonic(HanyuPinyinHelper.getFirstLettersLo(psPhysicFactoryDict.getFactory_name()));
		
		int r = this.dao.modify(psPhysicFactoryDict);
		if(r==1){
			return "操作成功!";
		}
		return "操作失败!";
	}
	
	public String delete(PsPhysicFactoryDict psPhysicFactoryDict) {
		int r = this.dao.delete(psPhysicFactoryDict);
		if(r==1){
			return "操作成功!";
		}
		return "操作失败!";
	}
	/**
	 * 新增和修改时校验
	 * @Description:TODO
	 * @author: chenliang
	 * @time:2018-3-18 上午11:27:07
	 */
	public void check(PsPhysicFactoryDict psPhysicFactoryDict){

		String fCode = psPhysicFactoryDict.getFactory_code();
		PsPhysicFactoryDict oldForQ = new PsPhysicFactoryDict();
		oldForQ.setFactory_code(fCode);
		List<PsPhysicFactoryDict> ppdL = dao.getBy(oldForQ);
		if (ppdL != null && ppdL.size() > 0) {
			throw new RuntimeException("厂家编号重复，请重新输入！"
					+ psPhysicFactoryDict.getFactory_code() + psPhysicFactoryDict.getFactory_name());
		}
	}

	/**
	 * 
	 */
	public List<PsPhysicFactoryDict> getForCombo(
			PsPhysicFactoryDict psPhysicFactoryDict) {
		
		List<PsPhysicFactoryDict> r = dao.getForCombo(psPhysicFactoryDict);
		
		return r;
	}
	
}
